/**
 * Filename: DateTimePickDialogUtil.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/18 15:17
 */
package com.github.postsanf.yinian.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.TimeCardEditActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期时间选择控件
 */
public class DateTimePickDialogUtil implements OnDateChangedListener {
    private DatePicker datePicker;
    private AlertDialog ad;
    private String dateTime;
    private String initDateTime;
    private Activity activity;

    /**
     * 日期时间弹出选择框构造函数
     *
     * @param activity     调用的父activity
     * @param initDateTime 初始日期时间值，作为弹出窗口的标题和日期时间初始值
     */
    public DateTimePickDialogUtil(Activity activity, String initDateTime) {
        this.activity = activity;
        this.initDateTime = initDateTime;
    }

    public void init(DatePicker datePicker) {
        Calendar calendar = Calendar.getInstance();
        if (!(null == initDateTime || "".equals(initDateTime))) {
            calendar = this.getCalendarByInintData(initDateTime);
        } else {
            initDateTime = calendar.get(Calendar.YEAR) + "-"
                    + calendar.get(Calendar.MONTH) + "-"
                    + calendar.get(Calendar.DAY_OF_MONTH);
        }
        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);
    }

    /**
     * 弹出日期时间选择框方法
     */
    public AlertDialog dateTimePicKDialog(final TextView inputDate) {
        // 自定义弹框布局
        View view = activity
                .getLayoutInflater().inflate(R.layout.yn_common_datetime, null);
        datePicker = (DatePicker) view.findViewById(R.id.datepicker);
        init(datePicker);

        TextView btn_left = (TextView) view.findViewById(R.id.btn_yn_datepicker_left);
        TextView btn_right = (TextView) view.findViewById(R.id.btn_yn_datepicker_right);
        ad = new AlertDialog.Builder(activity).show();
        ad.setCanceledOnTouchOutside(true);
        ad.setContentView(view);

        // 取消
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        // 确定
        btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputDate.setText(dateTime);
                ad.dismiss();
            }
        });
        onDateChanged(null, 0, 0, 0);
        WindowManager.LayoutParams layout = ad.getWindow().getAttributes();
        layout.width = DisplayUtils.getScreenWidthPixels(activity) - DisplayUtils.dp2px(activity, 32 * 2);
        ad.getWindow().setAttributes(layout);
        return ad;
    }

    public AlertDialog dateTimePicKDialog(final TextView inputyear, final TextView inputmouth) {
        // 自定义弹框布局
        View view = activity
                .getLayoutInflater().inflate(R.layout.yn_common_datetime, null);
        datePicker = (DatePicker) view.findViewById(R.id.datepicker);
        init(datePicker);

        TextView btn_left = (TextView) view.findViewById(R.id.btn_yn_datepicker_left);
        TextView btn_right = (TextView) view.findViewById(R.id.btn_yn_datepicker_right);
        ad = new AlertDialog.Builder(activity).show();
        ad.setCanceledOnTouchOutside(true);
        ad.setContentView(view);

        // 取消
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        // 确定
        btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] datestr = dateTime.split("-");
                inputyear.setTag(dateTime);
                inputyear.setText(datestr[0]);
                inputmouth.setText(datestr[1] + "." + datestr[2]);
                ad.dismiss();

            }
        });
        onDateChanged(null, 0, 0, 0);
        WindowManager.LayoutParams layout = ad.getWindow().getAttributes();
        layout.width = DisplayUtils.getScreenWidthPixels(activity) - DisplayUtils.dp2px(activity, 32 * 2);
        ad.getWindow().setAttributes(layout);
        return ad;
    }

    public AlertDialog dateTimePicTimeCardDialog(final TextView inputyear, final TextView inputmouth, final TextView inputri) {
        View view = activity.getLayoutInflater().inflate(R.layout.yn_common_datetime, null);
        datePicker = (DatePicker) view.findViewById(R.id.datepicker);
        init(datePicker);
        TextView btn_left = (TextView) view.findViewById(R.id.btn_yn_datepicker_left);
        TextView btn_right = (TextView) view.findViewById(R.id.btn_yn_datepicker_right);
        ad = new AlertDialog.Builder(activity).show();
        ad.setCanceledOnTouchOutside(true);
        ad.setContentView(view);
        // 取消
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        // 确定
        btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String[] datestr = dateTime.split("-");
                    Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(dateTime);
                    inputyear.setTag(dateTime);
                    inputyear.setText(datestr[0]);
                    inputmouth.setText(TimeCardEditActivity.mouthlist[date.getMonth()]);
                    inputri.setText("·" + datestr[2] + "·");
                    ad.dismiss();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        });
        onDateChanged(null, 0, 0, 0);
        WindowManager.LayoutParams layout = ad.getWindow().getAttributes();
        layout.width = DisplayUtils.getScreenWidthPixels(activity) - DisplayUtils.dp2px(activity, 32 * 2);
        ad.getWindow().setAttributes(layout);
        return ad;
    }

    public AlertDialog dateTimePicTimeMarksDialog(final TextView inputyear) {
        View view = activity.getLayoutInflater().inflate(R.layout.yn_common_datetime, null);
        datePicker = (DatePicker) view.findViewById(R.id.datepicker);
        init(datePicker);
        TextView btn_left = (TextView) view.findViewById(R.id.btn_yn_datepicker_left);
        TextView btn_right = (TextView) view.findViewById(R.id.btn_yn_datepicker_right);
        ad = new AlertDialog.Builder(activity).show();
        ad.setCanceledOnTouchOutside(true);
        ad.setContentView(view);
        // 取消
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        // 确定
        btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                    String[] datestr = dateTime.split("-");
//                    Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(dateTime);
                    inputyear.setTag(dateTime);
                    inputyear.setText(dateTime);
//                    inputmouth.setText(TimeCardEditActivity.mouthlist[date.getMonth()]);
//                    inputri.setText("·" + datestr[2] + "·");
                    ad.dismiss();
            }
        });
        onDateChanged(null, 0, 0, 0);
        WindowManager.LayoutParams layout = ad.getWindow().getAttributes();
        layout.width = DisplayUtils.getScreenWidthPixels(activity) - DisplayUtils.dp2px(activity, 32 * 2);
        ad.getWindow().setAttributes(layout);
        return ad;
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
        // 获得日历实例
        Calendar calendar = Calendar.getInstance();
        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        dateTime = sdf.format(calendar.getTime());
    }

    /**
     * 将初始日期时间 拆分成年 月 日,并赋值给calendar
     *
     * @param initDateTime 初始日期时间值 字符串型
     * @return Calendar
     */
    private Calendar getCalendarByInintData(String initDateTime) {
        Calendar calendar = Calendar.getInstance();
        String[] mdate = initDateTime.split("-");
        String yearStr = mdate[0];
        String monthStr = mdate[1];
        String dayStr = mdate[2];
        int currentYear = Integer.valueOf(yearStr.trim()).intValue();
        int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1;
        int currentDay = Integer.valueOf(dayStr.trim()).intValue();
        calendar.set(currentYear, currentMonth, currentDay);
        return calendar;
    }
}